/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utilities;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author olawale
 */
public class Utility {

    private static String [] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

    public static URI getURI(String uri) {
		try {
			return new URI(uri);
		} catch (URISyntaxException e) {
			return null; // Bad syntax
		}
	}

    public static Date parseDate(String dateString){
        //Sun Mar 18 06:42:26 +0000 2007
        if(dateString.length() < 15) return null;
        ArrayList<String> mn = new ArrayList();
        for(String m : months){
            mn.add(m);
        }
        String dates[] = dateString.split(" ");
        String times[] = dates[3].split(":");

        Calendar cal = Calendar.getInstance();
        int day = 0, month = 0, year = 0, hour = 0, minute = 0, second = 0;
        try{
            day = Integer.parseInt(dates[2]);
            month = mn.indexOf(dates[1]);
            year = Integer.parseInt(dates[5]);
            hour = Integer.parseInt(times[0]);
            minute = Integer.parseInt(times[1]);
            second = Integer.parseInt(times[2]);
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
        cal.set(year, month, day, hour, minute, second);
        return cal.getTime();
    }


}
